﻿using Newtonsoft.Json.Linq;

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using yyycommon;

namespace YYTmEditer.MtObject
{
    class QeuryObj {
        public string id = "";
    }
    public class RespObj {
        public string mt_id = "";
        public string mt_name="";
        public string mt_desc="";
        public string mt_group="";
        public int mt_lock;
        public int id = 0;

        public override string ToString()
        {
            return id+"-"+mt_name;
        }
    }
    public class EditObject
    {
        public string name="";
        public string group="";
        public string desc="";
        public string id = "";
        public string type = "";

        private bool locked;
        public EditObject(string value, string id, string type) {
            if (!string.IsNullOrEmpty(value)) {
                var arr = value.Split(new[] { '-' }, 2);
                name = arr[0];
                if (arr.Length > 1)
                {
                    desc = arr[1];
                }
            }
            this.id = id;
            this.type = type;
            

        }

        internal void load() {
            object resp = NetUtil.PostJson("http://fc.zwuyu.com/game/mtapi/query", new QeuryObj() { id = this.id });
            if (resp != null)
            {
                locked = true;
                JObject jobj = resp as JObject;
                if (jobj != null)
                {
                    var eobj = jobj.ToObject<RespObj>();
                    this.name = eobj.mt_name;
                    this.desc = eobj.mt_desc;
                }
            }
            else
            {
                locked = false;
            }
        }

        internal void Save(string text1, string text2,string group)
        {
            string oname = this.name.Trim() ;
            this.name = text1;
            this.desc = text2;
            this.group = group;

            if (locked || string.IsNullOrEmpty(text1) || text1.Equals(oname) )
            {
                return;
            }
            NetUtil.PostJson("http://fc.zwuyu.com/game/mtapi/push", this);
        }

        public override string ToString()
        {
            return name + "-" + desc;
        }

        public static List<RespObj> CardList() {
            var resp = NetUtil.PostJson("http://fc.zwuyu.com/game/mtapi/clist",null);
            if (resp != null) {
                var jobj = resp as JArray;
                if (jobj != null) {
                    return JsonConvert.DeserializeObject<List<RespObj>>(resp.ToString());
                }
            }
            return null;
        }
    }
}
